﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FrmConnRadiator : HYBaseForm
	{
		public FrmConnRadiator(Document document)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.userSetting = new HVACUserSetting();
			this.doc = document;
		}

		private void FrmConnRadiator_Load(object sender, EventArgs e)
		{
			this.InitialInterFace();
		}

		private void InitialInterFace()
		{
			ConnRadiatorSet connRadiatorSet = this.userSetting.GetConnRadiatorSet();
			if (connRadiatorSet.ConnStyle == 2)
			{
				this.SelectRadioButton(this.rdoDown);
			}
			else if (connRadiatorSet.ConnStyle == 3)
			{
				this.SelectRadioButton(this.rdoAcross);
			}
			else
			{
				this.SelectRadioButton(this.rdoDouble);
			}
			this.txtSupply.Text = connRadiatorSet.SupplyName;
			this.txtReturn.Text = connRadiatorSet.ReturnName;
			this.txtReturnLength.Text = connRadiatorSet.ReturnLength.ToString();
			this.txtSupplyLength.Text = connRadiatorSet.SupplyLength.ToString();
		}

		private void SaveInterFace()
		{
			ConnRadiatorSet connRadiatorSet = new ConnRadiatorSet();
			if (this.rdoAcross.Checked)
			{
				connRadiatorSet.ConnStyle = 3;
			}
			else if (this.rdoDown.Checked)
			{
				connRadiatorSet.ConnStyle = 2;
			}
			else
			{
				connRadiatorSet.ConnStyle = 1;
			}
			connRadiatorSet.SupplyName = this.txtSupply.Text;
			connRadiatorSet.ReturnName = this.txtReturn.Text;
			connRadiatorSet.ReturnLength = Convert.ToInt32(this.txtReturnLength.Text);
			connRadiatorSet.SupplyLength = Convert.ToInt32(this.txtSupplyLength.Text);
			this.userSetting.WriteConnRadiatorSet(connRadiatorSet);
			try
			{
				SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
				if (!string.IsNullOrEmpty(connRadiatorSet.SupplyName))
				{
					FamilySymbol familySymbolByNameSubTran = spaceFamilyDataReader.GetFamilySymbolByNameSubTran(connRadiatorSet.SupplyName, "PipeValve", this.doc, BuiltInCategory.OST_PipeAccessory);
					if (familySymbolByNameSubTran != null)
					{
						if (this.supplyAccessory == null)
						{
							this.supplyAccessory = new List<FamilySymbol>();
						}
						this.supplyAccessory.Add(familySymbolByNameSubTran);
					}
				}
				if (!string.IsNullOrEmpty(connRadiatorSet.ReturnName))
				{
					FamilySymbol familySymbolByNameSubTran2 = spaceFamilyDataReader.GetFamilySymbolByNameSubTran(connRadiatorSet.ReturnName, "PipeValve", this.doc, BuiltInCategory.OST_PipeAccessory);
					if (familySymbolByNameSubTran2 != null)
					{
						if (this.returnAccessory == null)
						{
							this.returnAccessory = new List<FamilySymbol>();
						}
						this.returnAccessory.Add(familySymbolByNameSubTran2);
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		private void SelectRadioButton(RadioButton button)
		{
			button.PerformClick();
			button.Select();
			button.Focus();
		}

		private void SelectedConnStyle()
		{
            System.Drawing.Color white = System.Drawing.Color.White;
            System.Drawing.Color deepSkyBlue = System.Drawing.Color.DeepSkyBlue;
			if (this.rdoDouble.Checked)
			{
				this.btnHDouble.BackColor = deepSkyBlue;
				this.btnLDouble.BackColor = deepSkyBlue;
				this.btnHDown.BackColor = white;
				this.btnLDown.BackColor = white;
				this.btnHAcross.BackColor = white;
				this.btnLAcross.BackColor = white;
				return;
			}
			if (this.rdoDown.Checked)
			{
				this.btnHDouble.BackColor = white;
				this.btnLDouble.BackColor = white;
				this.btnHDown.BackColor = deepSkyBlue;
				this.btnLDown.BackColor = deepSkyBlue;
				this.btnHAcross.BackColor = white;
				this.btnLAcross.BackColor = white;
				return;
			}
			if (this.rdoAcross.Checked)
			{
				this.btnHDouble.BackColor = white;
				this.btnLDouble.BackColor = white;
				this.btnHDown.BackColor = white;
				this.btnLDown.BackColor = white;
				this.btnHAcross.BackColor = deepSkyBlue;
				this.btnLAcross.BackColor = deepSkyBlue;
			}
		}

		private void btnConn_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
			this.SaveInterFace();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void rdoAcross_CheckedChanged(object sender, EventArgs e)
		{
			this.SelectedConnStyle();
		}

		private void btnSupply_Click(object sender, EventArgs e)
		{
			this.supplyAccessory = this.GetAccessory();
			if (this.supplyAccessory != null)
			{
				this.symbol = this.supplyAccessory[0];
				if (this.symbol.Family.Name.Length > 3 && this.symbol.Family.Name.Substring(0, 3).Equals("HY-"))
				{
					this.txtSupply.Text = this.symbol.Family.Name.Substring(3);
					return;
				}
				this.txtSupply.Text = this.symbol.Family.Name;
			}
		}

		private void btnReturn_Click(object sender, EventArgs e)
		{
			this.returnAccessory = this.GetAccessory();
			if (this.returnAccessory != null)
			{
				this.symbol = this.returnAccessory[0];
				if (this.symbol.Family.Name.Length > 3 && this.symbol.Family.Name.Substring(0, 3).Equals("HY-"))
				{
					this.txtReturn.Text = this.symbol.Family.Name.Substring(3);
					return;
				}
				this.txtReturn.Text = this.symbol.Family.Name;
			}
		}

		private List<FamilySymbol> GetAccessory()
		{
			FamilySymbol familySymbol = new ILayForm(this.doc).LayPipeAccessoryFormShow(this.doc, "水管阀件", "lay", true);
			List<FamilySymbol> list = null;
			if (familySymbol != null)
			{
				list = new List<FamilySymbol>();
				list.Add(familySymbol);
			}
			return list;
		}

		private void txtSupply_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			TextBox textBox = sender as TextBox;
			if (textBox.Name == "txtSupply" && !string.IsNullOrEmpty(textBox.Text))
			{
				this.txtSupply.Text = "";
				this.supplyAccessory = null;
			}
			if (textBox.Name == "txtReturn" && !string.IsNullOrEmpty(textBox.Text))
			{
				this.txtReturn.Text = "";
				this.returnAccessory = null;
			}
		}

		public FamilySymbol GetSupplyAccessory()
		{
			FamilySymbol result = null;
			if (!string.IsNullOrEmpty(this.txtSupply.Text) && this.supplyAccessory != null)
			{
				result = this.supplyAccessory[0];
			}
			return result;
		}

		public FamilySymbol GetReturnAccessory()
		{
			FamilySymbol result = null;
			if (!string.IsNullOrEmpty(this.txtReturn.Text) && this.returnAccessory != null)
			{
				result = this.returnAccessory[0];
			}
			return result;
		}

		public int GetSupplyLength()
		{
			return Convert.ToInt32(this.txtSupplyLength.Text);
		}

		public int GetReturnLength()
		{
			return Convert.ToInt32(this.txtReturnLength.Text);
		}

		public int GetConnStyle()
		{
			int result = 1;
			if (this.rdoAcross.Checked)
			{
				result = 3;
			}
			else if (this.rdoDown.Checked)
			{
				result = 2;
			}
			return result;
		}

		private void txtSupplyLength_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (string.IsNullOrEmpty(hytextBox.Text))
			{
				YJKMessageBox.Warning("长度设置不能为空！");
				e.Cancel = true;
			}
			try
			{
				int num = Convert.ToInt32(hytextBox.Text);
				if (num <= 0 || num > 5000)
				{
					YJKMessageBox.Warning("长度设置值应该在0-5000之间！");
					e.Cancel = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Warning("长度设置应为正整数！");
				e.Cancel = true;
			}
		}

		private HVACUserSetting userSetting;

		private Document doc;

		private List<FamilySymbol> supplyAccessory;

		private List<FamilySymbol> returnAccessory;

		private FamilySymbol symbol;
	}
}
